<?php

namespace App\Http\Controllers\Api\Trip;

use App\Facades\Plane\PlaneCitySeedFacade;
use App\Http\Controllers\Controller;
use App\Http\Resources\Train\HistorySource;
use App\Http\Resources\Train\HotResource;
use App\Models\Plane\Airports\Repositories\Interfaces\AirportRepositoryInterface;
use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Models\Trip\History\Repositories\Interfaces\TripQueryHistoryRepositoryInterface;
use App\Models\User\Repositories\Interfaces\UserRepositoryInterface;
use App\Trip\Orders\TripType;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

class HistoryController extends Controller
{
    private StationRepositoryInterface          $stationRepo;
    private TripQueryHistoryRepositoryInterface $historyRepo;
    private AirportRepositoryInterface          $airportRepo;
    private string|int                          $user_id;

    public function __construct(
        StationRepositoryInterface          $stationRepository,
        AirportRepositoryInterface          $airportRepository,
        TripQueryHistoryRepositoryInterface $historyRepository,
        UserRepositoryInterface             $userRepository
    )
    {
        $this->stationRepo = $stationRepository;
        $this->airportRepo = $airportRepository;
        $this->historyRepo = $historyRepository;
        $this->user_id     = auth()->id() ?: '';
    }

    public function list(Request $request)
    {
        $list = $this->user_id ? $this->historyRepo->listByUserId($this->user_id, TripType::TRIP_TYPE_TRAIN) : [];
        $hot  = $this->getHotStations();
        return $this->success('', [
            'history' => HistorySource::collection($list),
            'hot'     => HotResource::collection($hot->toArray()),
        ]);
    }

    /**
     * @return Collection
     */
    private function getHotStations(): Collection
    {
        return collect($this->stationRepo->listHotStations(6))->map(fn($station) => [
            'type'    => 2,
            'code'    => $station->station_code,
            'name'    => $station->station_name,
            'name_uy' => $station->station_name_uy,
            'sorts'   => $station->sorts,
        ]);
    }

    public function airport_list(Request $request)
    {

        $list       = $this->user_id ? $this->historyRepo->listByUserId($this->user_id, TripType::TRIP_TYPE_PLANE) : [];
        $hot_cities = $this->getHotAirportCities();
        $hot        = $this->getHotAirports();
        return $this->success('', [
            'history'    => HistorySource::collection($list),
            'hot'        => HotResource::collection($hot),
            'hot_cities' => HotResource::collection($hot_cities),
        ]);
    }

    private function getHotAirportCities(): Collection
    {
        return collect($this->airportRepo->listHotCities(6))
            ->map(fn($airport) => [
                'type'        => 0,
                'code'        => $airport->city_code,
                'sorts'       => $airport->sorts,
                'name'        => PlaneCitySeedFacade::getCityNameByCode($airport->city_code),
                'name_uy'     => PlaneCitySeedFacade::getCityNameUyByCode($airport->city_code),
                'city_adcode' => PlaneCitySeedFacade::getCityAdcodeByCode($airport->city_code),
            ]);
    }

    private function getHotAirports(): Collection
    {
        return collect($this->airportRepo->listHotAirports(6))
            ->map(fn($airport) => [
                'type'        => 2,
                'code'        => $airport->code,
                'name'        => $airport->name,
                'name_uy'     => $airport->name_uy,
                'sorts'       => $airport->sorts,
                'city_adcode' => PlaneCitySeedFacade::getCityAdcodeByCode($airport->city_code),
            ]);
    }

    public function hot()
    {
        $hot = $this->getHotStations();
        return $this->success('', HotResource::collection($hot));
    }

    public function airport_hot()
    {
        $hot = $this->getHotAirports();
        return $this->success('', HotResource::collection($hot));
    }


    public function airport_cities_hot()
    {
        $hot = $this->getHotAirportCities();
        return $this->success('', HotResource::collection($hot));
    }

    public function delete()
    {
        $this->historyRepo->clearByUserId($this->user_id, TripType::TRIP_TYPE_TRAIN);
        return $this->success();
    }

    public function airport_delete()
    {
        $this->historyRepo->clearByUserId($this->user_id, TripType::TRIP_TYPE_PLANE);
        return $this->success();
    }
}
